<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
class LM_Table_Match extends LM_Table
{
    /** @var int Primary key */
    public $match_id;
    /** @var int references LM_Table_Team */
    public $match_team1;
    /** @var int references LM_Table_Team */
    public $match_team2;
    /** @var int EndResult Team1 */
    public $match_result_0_1;
    /** @var int EndResult Team2 */
    public $match_result_0_2;
    /** @var int Result1 Team1 */
    public $match_result_1_1;
    /** @var int Result1 Team2 */
    public $match_result_1_2;
    /** @var int Result2 Team1 */
    public $match_result_2_1;
    /** @var int Result2 Team2 */
    public $match_result_2_2;
    /** @var int Result3 Team1 */
    public $match_result_3_1;
    /** @var int Result3 Team2 */
    public $match_result_3_2;
    /** @var int Result4 Team1 */
    public $match_result_4_1;
    /** @var int Result4 Team2 */
    public $match_result_4_2;
    /** @var int Result5 Team1 */
    public $match_result_5_1;
    /** @var int Result5 Team2 */
    public $match_result_5_2;
    /** @var int 0 = regular, 1 = addtime, 2 = penalty */
    public $match_result_type;
    /** @var string */
    public $match_result_info;
    /** @var int references LM_Table_Project */
    public $match_project;
    /** @var int references LM_Table_Round */
    public $match_round;
    /** @var int references LM_Table_Group */
    public $match_group;
    /** @var string */
    public $match_venue;
    /** @var string */
    public $match_referee;
    /** @var date */
    public $match_date;
    /** @var time */
    public $match_time;
    /** @var bool more info available? */
    public $match_info_flag;
    /** @var bool published for ranking? */
    public $published;
    /** @var int count of spectators */
    public $match_crowd;
    private $_round;
    private $_team1;
    private $_team2;
    private $_project;

    public function __construct($db)
    {
        parent::__construct('#__leagueman_match', 'match_id', $db);
    }

    public function getRound()
    {
        if (!$this->_round instanceof LM_Table_Round) {
            $this->_round = LM::getLMTable("Round");
            $this->_round->load($this->match_round);
        }

        return $this->_round;
    }

    public function getLineup(LM_Table_Team $team)
    {

        $query = "SELECT lineup_player
                    FROM #__leagueman_lineup
                   WHERE lineup_match = %s
                     AND lineup_team = %s";
        $this->_db->setQuery(sprintf($query, $this->match_id, $team->getID()));
        return $this->_db->loadResultArray();
    }

    public function getLineupData(LM_Table_Team $team)
    {

        $query = "SELECT p.*
                    FROM #__leagueman_player p, #__leagueman_lineup l
                   WHERE l.lineup_player = p.player_id
                     AND l.lineup_match = %s
                     AND l.lineup_team = %s";
        $this->_db->setQuery(sprintf($query, $this->match_id, $team->getID()));
        return $this->_db->loadObjectList();
    }

    public function getTeam1()
    {
        if (!$this->_team1 instanceof LM_Table_Team) {
            $this->_team1 = LM::getLMTable("Team");
            $this->_team1->load($this->match_team1);
        }

        return $this->_team1;
    }

    public function getTeam2()
    {
        if (!$this->_team2 instanceof LM_Table_Team) {
            $this->_team2 = LM::getLMTable("Team");
            $this->_team2->load($this->match_team2);
        }

        return $this->_team2;
    }

    public function getProject()
    {
        if (!$this->_project instanceof LM_Table_Project) {
            $this->_project = LM::getLMTable("Project");
            $this->_project->load($this->match_project);
        }

        return $this->_project;
    }

    public function getMatchDate($format = "Y-m-d")
    {
        
    }

    public function getMatchTime($format = "H:i")
    {

    }

    public function getResult($v = 0)
    {
        $e1 = "match_result_{$v}_1";
        $e2 = "match_result_{$v}_2";

        if (is_numeric($this->$e1) && is_numeric($this->$e2)) {
            return sprintf("%s:%s", $this->$e1, $this->$e2);
        } else {
            return "-:-";
        }
    }

    public function getEvents()
    {
        $query = "SELECT e.*, p.player_id AS playerid, p.player_name AS playername
                    FROM #__leagueman_event e
              INNER JOIN #__leagueman_playermap pm ON pm.playermap_id = e.event_playermap
              INNER JOIN #__leagueman_player p ON pm.playermap_player = p.player_id
                   WHERE e.event_match = {$this->match_id}
                ORDER BY e.event_minute";
        $this->_db->setQuery($query);
        return $this->_db->loadObjectList();
    }

    public function getRows($all = true, $limitstart = 0, $limit = 10)
    {
        $sql = "SELECT m.*,
                       t1.team_id AS team1_id, t1.team_name AS team1_name, t1.team_name_short AS team1_name_short, t1.team_logo AS team1_logo,
                       t2.team_id AS team2_id, t2.team_name AS team2_name, t2.team_name_short AS team2_name_short, t2.team_logo AS team2_logo
                  FROM #__leagueman_match m
            INNER JOIN #__leagueman_team t1 ON t1.team_id = m.match_team1
            INNER JOIN #__leagueman_team t2 ON t2.team_id = m.match_team2"
             . $this->getFilter() . "
              ORDER BY m.match_round, m.match_date, m.match_time";

        if (!$all) {
            $this->_db->setQuery($sql, $limitstart, $limit);
        } else {
            $this->_db->setQuery($sql);
        }

        return $this->_db->loadObjectList();
    }

    public function getResults($mode = 0)
    {
        $sql = "SELECT m.match_id,
					   m.match_result_0_1 AS e1,
					   m.match_result_0_2 AS e2,
					   m.match_result_type AS type,
					   m.published,
					   t1.team_id AS team1_id, t1.team_name AS team1_name, t1.team_name_short AS team1_name_short, t1.team_logo AS team1_logo,
					   t2.team_id AS team2_id, t2.team_name AS team2_name, t2.team_name_short AS team2_name_short, t2.team_logo AS team2_logo
				  FROM #__leagueman_match m
			INNER JOIN #__leagueman_team t1 ON t1.team_id = m.match_team1
			INNER JOIN #__leagueman_team t2 ON t2.team_id = m.match_team2"
                . $this->getFilter() . "
			  ORDER BY m.match_round, m.match_date, m.match_time";

        $this->_db->setQuery($sql);

        return $this->_db->loadObjectList();
    }

    public function getTotalRows()
    {
        $sql = "SELECT COUNT(m.match_id)
                  FROM #__leagueman_match m
            INNER JOIN #__leagueman_team t1 ON t1.team_id = m.match_team1
            INNER JOIN #__leagueman_team t2 ON t2.team_id = m.match_team2"
             . $this->getFilter();

        $this->_db->setQuery($sql);
        return $this->_db->loadResult();
    }

    public function filterbyProject($project_id)
    {
        $this->addFilter("m.match_project = " . $this->_db->Quote(intval($project_id)));
    }

    public function filterbyTeam($team_id, $mode = 0)
    {

        if ($team_id > 0) {
            $team = $this->_db->Quote(intval($team_id));

            switch ($mode) {
                case 0:
                    $this->addFilter("(m.match_team1 = " . $team . " OR m.match_team2 = " . $team . ")");
                    break;

                case 1:
                    $this->addFilter("m.match_team1 = " . $team);
                    break;

                case 2:
                    $this->addFilter("m.match_team2 = " . $team);
                    break;
            }
        }
    }

    public function filterbyRound($ordering)
    {
        if ($ordering > 0) {
            $this->addFilter("m.match_round = " . $this->_db->Quote(intval($ordering)));
        }
    }

    public function filterbyGroup($group = '')
    {
        if (!empty($group)) {
            $this->addFilter("m.match_group = " . $this->_db->Quote($group));
        }
    }

    public function filterbyMatch($match_id)
    {
        $this->addFilter("m.match_id = " . $this->_db->Quote(intval($match_id)));
    }

    public function filterbyPublished()
    {
        $this->addFilter("m.published = 1");
    }

    public function filterbyMatchDate($from, $to = '', $delta = 2)
    {
        $from = (ereg("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $from)) ? $this->_db->Quote($from) : 0;
        $to = (ereg("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $to)) ? $this->_db->Quote($to) : $from;

        if ($from) {

            $from = "DATE_SUB($from, INTERVAL $delta DAY)";
            $to = "DATE_ADD($to, INTERVAL $delta DAY)";

            $this->addFilter("(m.match_date BETWEEN " . $from . " AND " . $to . ")");
        }
    }

    public function filterByCategory($category_id)
    {
        $this->addFilter("p.project_category = " . $this->_db->Quote(intval($category_id)));
    }

}